<template>
  <div class="boxx">
    <p>请填写【{{form.clientName}}】的理赔信息:</p>

    <el-form ref="form" :model="form" label-width="80px">
    <el-form-item label="上传凭证">
      <!-- 凭证上传 -->
      <el-upload
        class="avatar-uploader"
        action="http://localhost:3000/images/upload"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload"
      >
        <img
          v-if="form.imgname"
          :src="'http://localhost:3000/temp/'+form.imgname"
          class="avatar"
        />
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
      <!-- 凭证上传 -->
    </el-form-item>
    <el-form-item label="客户姓名">
      <el-input v-model="form.clientName" ></el-input>
    </el-form-item>
    <el-form-item label="所购保险">
      <el-input v-model="form.insuranceName" ></el-input>
    </el-form-item>
    </el-form-item>
    <el-form-item label="保险价格">
      <el-input v-model="form.insurancePirce"></el-input>
    </el-form-item>
    <el-form-item label="保险期限">
      <el-input  v-model="form.timeLength"></el-input>
    </el-form-item>
   <el-form-item label="理賠金额">
      <el-input  v-model="form.payment"></el-input>
    </el-form-item>
    <el-form-item label="理赔描述">
      <el-input v-model="form.reason"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="addLipeiData">上传</el-button>
    </el-form-item>
  </el-form>
  </div>
  
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const { mapActions } = createNamespacedHelpers("order");
const {mapActions:LipeiMapAction} = createNamespacedHelpers("lipei");
export default {
  data() {
    return {
      form: {
        clientName:'',
        insuranceName: '',
        insurancePirce: '',
        imgname:'',
        payment:'',
        reason:'',
        timeLength:'',
        salesId:'',
      },
      clientname:'',
    };
  },
  async created() {
    const _id = this.$route.params._id;
    // console.log(_id);
    const data = await this.getOrder(_id);
    // console.log(data);
    this.form.clientName =data.clientId.username; 
    this.form.insuranceName = data.insurancesId.name;
    this.form.insurancePirce = data.insurancesId.price;
    this.form.payment = data.insurancesId.payment;
    this.form.timeLength = data.insurancesId.duration;
    this.form.salesId = data.salesId._id
  },
  methods: {
    ...mapActions(["getOrder",]),
    ...LipeiMapAction(["addLipeiAsync"]),
    //增加理赔信息

    async addLipeiData(){
      const data = await this.addLipeiAsync(this.form);
      if(data.status){
          this.$message({
          message: data.message,
          type: 'success'
        });
      }else{
        this.$message.error(data.message);
      }
      
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      console.log(res);
      if (res.status) {
        this.form.imgname = res.data;
        // console.log(this.form.imgname);
      }
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style>
.boxx{
  width: 400px;
  margin: 0 auto;
}
.avatar-uploader .el-upload {
  border: 1px dashed #ffffff;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  background-color: #ffffff;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #ddd9d9;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>